﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.IO;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Windows.Media.Animation;

namespace ImagEye
{
    public partial class wndMain : Window
    {
        private string _fileMask = "*.jpg";

        private string directory;
        public string Directory
        {
            get { return directory; }
            set { directory = value; }
        }

        private string[] fileList = null;
        public string[] FileList
        {
            get { return fileList; }
            set { fileList = value; }
        }

        private int imageIndex;
        public int ImageIndex
        {
            get { return imageIndex; }
            set { imageIndex = value; }
        }

        private int _rotationAngle;
        public int RotationAngle
        {
            get { return _rotationAngle; }
            set { _rotationAngle = value; }
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
        }

        public wndMain()
        {
            InitializeComponent();
        }

        private void imgLiveViewer_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                _rotationAngle = _rotationAngle + 5;

                DoubleAnimation da = new DoubleAnimation();
                da.From = _rotationAngle - 5;
                da.To = _rotationAngle;
                da.AccelerationRatio = 0.50;
                da.DecelerationRatio = 0.50;
                da.Duration = new Duration(TimeSpan.FromSeconds(1));

                RotateTransform rt = new RotateTransform();
                imgLiveViewer.RenderTransformOrigin = new Point(0.5, 0.5);

                imgLiveViewer.RenderTransform = rt;
                rt.BeginAnimation(RotateTransform.AngleProperty, da);

                if ((_rotationAngle) > 359)
                    _rotationAngle = 0;
            }
            else if (e.Delta < 0)
            {
                _rotationAngle = _rotationAngle - 5;

                DoubleAnimation da = new DoubleAnimation();
                da.From = _rotationAngle;
                da.To = _rotationAngle - 5;
                da.AccelerationRatio = 0.50;
                da.DecelerationRatio = 0.50;
                da.Duration = new Duration(TimeSpan.FromSeconds(1));

                RotateTransform rt = new RotateTransform();
                imgLiveViewer.RenderTransformOrigin = new Point(0.5, 0.5);

                imgLiveViewer.RenderTransform = rt;
                rt.BeginAnimation(RotateTransform.AngleProperty, da);

                if ((_rotationAngle) > 359)
                    _rotationAngle = 0;
            }
            this.Focus();
        }

        private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            FolderBrowserDialog d = new FolderBrowserDialog();
            d.RootFolder = Environment.SpecialFolder.MyComputer;
            d.ShowDialog();
            if (d.SelectedPath.Length > 0)
            {
                directory = d.SelectedPath;
                fileList = System.IO.Directory.GetFiles(directory, _fileMask);

                if (fileList.Count() == 0)
                    return;

                imageIndex = 0;
                ImageSourceConverter imageSrcConverter = new ImageSourceConverter();
                imgLiveViewer.Source = (ImageSource)imageSrcConverter.ConvertFromString(fileList[imageIndex]);
            }
        }

        private void imgLiveViewer_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.PageDown || e.Key == Key.Next)
            {
                imageIndex++;

                if (imageIndex < 0 || fileList == null || imageIndex > fileList.Count())
                {
                    imageIndex = 0;
                    return;
                }
                ImageSourceConverter c = new ImageSourceConverter();
                imgLiveViewer.Source = (ImageSource)c.ConvertFromString(this.fileList[imageIndex]);
            }
            else if (e.Key == Key.PageUp || e.Key == Key.Prior)
            {
                imageIndex--;

                if (imageIndex < 0 || fileList == null || imageIndex > fileList.Count())
                {
                    imageIndex = 0;
                    return;
                }

                ImageSourceConverter c = new ImageSourceConverter();
                imgLiveViewer.Source = (ImageSource)c.ConvertFromString(fileList[imageIndex]);

            }
            else if (e.Key == Key.F)
            {
                if (this.WindowStyle == WindowStyle.None)
                    this.WindowStyle = WindowStyle.SingleBorderWindow;
                else
                    this.WindowStyle = WindowStyle.None;
            }
            else if (e.Key == Key.Escape)
            {
                Close();
            }
            else if (e.Key == Key.Add)
            {
                Random rnd = new Random();

                ScaleTransform sct = new ScaleTransform(1.5, 1.5);

                imgLiveViewer.RenderTransformOrigin = new Point(rnd.NextDouble(), rnd.NextDouble());

                imgLiveViewer.RenderTransform = sct;

                DoubleAnimation da = new DoubleAnimation(1, new Duration(TimeSpan.FromSeconds(8)));

                sct.BeginAnimation(ScaleTransform.ScaleXProperty, da);

                sct.BeginAnimation(ScaleTransform.ScaleYProperty, da);
            }
        }

    }
}
